﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows.Data;
using GTServices.Model;
using Caliburn.Micro;
using GTServices.Entities;
using GTServices.Model.Extensions;
using GTServices.WPF.Client.Interfaces;


namespace GTServices.WPF.Client.ViewModels.Entities
{
    public class GrepResultViewModel : PropertyChangedBase, IEntityViewModel
    {
        private GrepResult _grepResult { get; set; }
        public GrepResult GrepResult
        {
            get { return _grepResult; }
            set { _grepResult = value; NotifyOfPropertyChange(() => GrepResult); } 
        }
        public Object Model
        {
            get { return GrepResult; }
            set
            {
                if (value is GrepResult)
                    GrepResult = value as GrepResult;
            }
        }
        public MatchInfo SelectedMatchInfo { get; set; }

        public ListCollectionView MatchInfos
        {
            get
            {
                var lcv = new ListCollectionView(ModelViewModels.Instance.MatchInfoViewModels);
                lcv.Filter += new Predicate<object>(x =>
                {
                    MatchInfoViewModel mivm = x as MatchInfoViewModel;
                    if (mivm != null && GrepResult != null && GrepResult.MatchInfos != null && GrepResult.MatchInfos.Contains(mivm.MatchInfo))
                        return true;

                   
                    return false;
                });
                lcv.Refresh(); 
                return lcv;
            }
        }


        public GrepResultViewModel()
        {
          
        }

        /*
        public void RemoveCategory(ActionExecutionContext context)
        {
            if (context.Source is MenuItem)
            {
                Category i = (context.Source as MenuItem).CommandParameter as Category;
                if(i != null){
                    this.Categories.Remove(i);
                    this.Categories.Refresh(); 
                    NotifyOfPropertyChange(() => Categories); 
                }
            }
        }

        public void SelectedItemChanged(ActionExecutionContext context)
        {
            SelectionChangedEventArgs eventArgs = context.EventArgs as SelectionChangedEventArgs;
            if (eventArgs != null)
            {
                eventArgs.RemovedItems.ForEach((x) =>
                {
                    SelectedItems.Remove(x);
                });

                eventArgs.AddedItems.ForEach((x) =>
                {
                    SelectedItems.Add(x);
                });
            }
        }
        public void MarkReviewed(ActionExecutionContext context)
        {
            if (this.SelectedItems != null && this.SelectedItems.Count > 0)
            {
                var items = this.SelectedItems.Cast<MatchInfo>().ToList();
                foreach (MatchInfo mi in items)
                {
                    mi.Reviewed = !mi.Reviewed; 
                }
                this.MatchInfos.Refresh();
            }
        }
        public void MarkFinding(ActionExecutionContext context)
        {
            if (this.SelectedItems != null && this.SelectedItems.Count > 0)
            {
                var items = this.SelectedItems.Cast<MatchInfo>().ToList();
                foreach (MatchInfo mi in items)
                {
                    mi.IsFinding = !mi.IsFinding;
                }
                this.MatchInfos.Refresh();
            }
        }
        public void RemoveMatchInfo(ActionExecutionContext context)
        {
            if(this.SelectedItems != null && this.SelectedItems.Count > 0){
                var items = this.SelectedItems.Cast<MatchInfo>().ToList(); 
                foreach (MatchInfo mi in items)
                {
                    this.MatchInfos.Remove(mi);
                }
                this.MatchInfos.Refresh();
            }
        }
       
        public void ShowResultInWin(ActionExecutionContext context)
        {
            if (this.SelectedMatchInfo != null)
            {
                string ext = SelectedMatchInfo.FileInfo.Extension.Extension;

                MatchInfoViewModel rvm = new MatchInfoViewModel(this);
                rvm.MatchInfo = this.SelectedMatchInfo;
                GTWindowManager winMgr = new GTWindowManager();
                winMgr.ShowWindow(rvm, 500, 1000);
              
            }
        }
 */
        public void SortClick(ActionExecutionContext context)
        {
            Button b = context.Source as Button;

            ListCollectionView lcv = (context.View as ListView).ItemsSource as ListCollectionView;
            if (b == null || lcv == null)
            {
                //TODO: Log + Error
                return;
            }

            switch (b.Content.ToString())
            {

                case "File":
                    lcv.SortDescriptions.Clear();
                    lcv.SortDescriptions.Add(new System.ComponentModel.SortDescription("FileInfo.Name", System.ComponentModel.ListSortDirection.Ascending));
                    break;
                case "Line#":
                    lcv.SortDescriptions.Clear();
                    lcv.SortDescriptions.Add(new System.ComponentModel.SortDescription("LineNumber", System.ComponentModel.ListSortDirection.Ascending));
                    break;
                case "Pattern":
                    lcv.SortDescriptions.Clear();
                    lcv.SortDescriptions.Add(new System.ComponentModel.SortDescription("Pattern.PatternStr", System.ComponentModel.ListSortDirection.Ascending));
                    break;
                case "Context":

                    lcv.SortDescriptions.Clear();
                    lcv.SortDescriptions.Add(new System.ComponentModel.SortDescription("Line", System.ComponentModel.ListSortDirection.Ascending));
                    break;
                case "Fulle Path":

                    lcv.SortDescriptions.Clear();
                    lcv.SortDescriptions.Add(new System.ComponentModel.SortDescription("FileInfo.FullName", System.ComponentModel.ListSortDirection.Ascending));
                    break;
                default:
                    break;
            }
            return;
        }

    }
}
